package org.aplikator.server.convertor;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

public class Convertor {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) throws Exception {

        /* ------------------------------------------------------------------- */
        /* You should do this ONLY ONCE in the whole application life-cycle:   */

        /* Create and adjust the configuration */
        Configuration cfg = new Configuration();
        cfg.setClassForTemplateLoading(Convertor.class, "");
        cfg.setObjectWrapper(new DefaultObjectWrapper());

        /* ------------------------------------------------------------------- */
        /* You usually do these for many times in the application life-cycle:  */

        /* Get or create a template */
        Template temp = cfg.getTemplate("Entity.ftl");


        File rootDir = new File("/Work/SA/SAK-def/defs/desks/cz/incad/skodaauto/knihovna");
        File targetDir = new File("/Work/workspace/skoda/src/cz/incad/skoda/server/knihovna");
        targetDir.mkdirs();
        for (File f : rootDir.listFiles()){
            @SuppressWarnings("rawtypes")
            Map root = new HashMap();
            root.put(
                    "doc",
                    freemarker.ext.dom.NodeModel.parse(f));

            File targetFile = new File(targetDir,f.getName().replace(".def", ".java"));
            targetFile.createNewFile();
            /* Merge data-model with template */
            Writer out = new FileWriter(targetFile);
            //out.write("\n############################### " + f.getName().replace(".def", ".java") +" #######################\n\n");
            temp.process(root, out);
            out.flush();
            out.close();
        }
    }
}